<?php

namespace App\Console;

use App\Services\AutoService;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        //
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        //12点执行发放饭点打卡活动奖金--每天12点执行
        $schedule->call(function () {
            try {
                AutoService::mealLotGrant();
            }catch (\Exception $exception){
                InterfaceLog('mealLotGrant', '12点执行发放饭点打卡活动奖金异常'.$exception->getMessage(), 2);
            }
        })->dailyAt('12:00');

        //订单30分钟自动过期--每分钟执行
        $schedule->call(function () {
            try {
                AutoService::orderAutoCancel();
            }catch (\Exception $exception){
                InterfaceLog('orderAutoCancel', '订单30分钟自动过期异常'.$exception->getMessage(), 2);
            }

        })->everyMinute();

        //任务自动过期--00：00执行
        $schedule->call(function () {
            try {
                AutoService::subAutoCancel();
            }catch (\Exception $exception){
                InterfaceLog('subAutoCancel', '任务自动过期异常'.$exception->getMessage(), 2);
            }

        })->daily()->after(function () {
            // 任务完成之后，执行任务的剩余单数重置
            try {
                AutoService::subCountReset();
            }catch (\Exception $exception){
                InterfaceLog('subCountReset', '剩余单数重置异常'.$exception->getMessage(), 2);
            }

        });;

        //订单初审核自动审核--每分钟执行
        $schedule->call(function () {
            try {
                AutoService::orderAudit();
            }catch (\Exception $exception){
                InterfaceLog('orderAudit', '订单自动审核异常'.$exception->getMessage(), 2);
            }
        })->everyMinute();

        //初审上传提醒--每分钟执行
        $schedule->call(function () {
            try {
                AutoService::checkOrderOverdue();
            }catch (\Exception $exception){
                InterfaceLog('checkOrderOverdue', '初审上传提醒异常'.$exception->getMessage(), 2);
            }
        })->everyMinute();

        //订单初审通过提醒--每分钟执行
        $schedule->call(function () {
            try {
                AutoService::orderFirstOkRemind();
            }catch (\Exception $exception){
                InterfaceLog('orderFirstOkRemind', '订单初审通过提醒异常'.$exception->getMessage(), 2);
            }
        })->everyMinute();

        //订单初审失败提醒--每分钟执行
        $schedule->call(function () {
            try {
                AutoService::orderFirstFailRemind();
            }catch (\Exception $exception){
                InterfaceLog('orderFirstFailRemind', '订单初审失败提醒异常'.$exception->getMessage(), 2);
            }
        })->everyMinute();

        //订单复审失败提醒--每分钟执行
        $schedule->call(function () {
            try {
                AutoService::orderSecondFailRemind();
            }catch (\Exception $exception){
                InterfaceLog('orderSecondFailRemind', '订单复审未通过提醒异常'.$exception->getMessage(), 2);
            }
        })->everyMinute();

        //订单完成提醒--每分钟执行
        $schedule->call(function () {
            try {
                AutoService::orderCompleteRemind();
            }catch (\Exception $exception){
                InterfaceLog('orderCompleteRemind', '订单完成提醒异常'.$exception->getMessage(), 2);
            }
        })->everyMinute();

        //订单次日16点前未提交评价图则取消--16：00执行
        $schedule->call(function () {
            try {
                AutoService::orderAuditCancel();
            }catch (\Exception $exception){
                InterfaceLog('orderAuditCancel', '订单次日16点前未提交评价图则取消异常'.$exception->getMessage(), 2);
            }

        })->dailyAt('16:00');

        //订单次日13点前未提交复审提醒--13：00执行
        $schedule->call(function () {
            try {
                AutoService::orderTomoNotSubmitAudit();
            }catch (\Exception $exception){
                InterfaceLog('orderTomoNotSubmitAudit', '订单次日13点前未提交复审提醒异常'.$exception->getMessage(), 2);
            }

        })->dailyAt('13:00');

        //复审即将逾期通知（商家）（每日14、21点通知有待审核订单商户）
        $schedule->call(function () {
            try {
                AutoService::orderNotSettleNotify();
            }catch (\Exception $exception){
                InterfaceLog('orderNotSettleNotify', '复审即将逾期通知异常'.$exception->getMessage(), 2);
            }

        })->twiceDaily(14, 21);

        //计算前一天对账明细--00：00执行
        $schedule->call(function () {
            try {
                AutoService::PlatformDayBalanceLog();
            }catch (\Exception $exception){
                InterfaceLog('PlatformDayBalanceLog', '计算前一天对账明细异常'.$exception->getMessage(), 2);
            }

        })->daily();

        //计算今天之前当月对账明细--00：00执行
        $schedule->call(function () {
            try {
                AutoService::PlatformMonthBalanceLog();
            }catch (\Exception $exception){
                InterfaceLog('PlatformMonthBalanceLog', '计算今天之前当月对账明细异常'.$exception->getMessage(), 2);
            }

        })->daily();

        //商户待结算检查--00：00执行
        $schedule->call(function () {
            try {
                AutoService::notSettleCheck();
            }catch (\Exception $exception){
                InterfaceLog('notSettleCheck', '商户待结算检查异常'.$exception->getMessage(), 2);
            }

        })->daily();

        //计算平台余额--每分钟执行
        $schedule->call(function () {
            try {
                AutoService::cacluPlatformBalance();
            }catch (\Exception $exception){
                InterfaceLog('cacluPlatformBalance', '计算平台余额异常'.$exception->getMessage(), 2);
            }
        })->everyMinute();

        //计算用户余额--每分钟执行
        $schedule->call(function () {
            try {
                AutoService::cacluUserBalance();
            }catch (\Exception $exception){
                InterfaceLog('cacluUserBalance', '计算用户余额异常'.$exception->getMessage(), 2);
            }
        })->everyMinute();

        //用户vip到期
        $schedule->call(function () {
            try {
                AutoService::userAutoOverdue();
            }catch (\Exception $exception){
                InterfaceLog('userAutoOverdue', '用户vip到期异常'.$exception->getMessage(), 2);
            }
        })->everyMinute();

        //提现结果查询
        $schedule->call(function () {
            try {
                AutoService::withdrawalResultCheck();
            }catch (\Exception $exception){
                InterfaceLog('withdrawalResultCheck', '提现结果查询异常'.$exception->getMessage(), 2);
            }
        })->everyMinute();

        //微信提现处理中的结果查询
        $schedule->call(function () {
            try {
                AutoService::withdrawalProcessingResultCheck();
            }catch (\Exception $exception){
                InterfaceLog('withdrawalProcessingResultCheck', '微信提现处理中的结果查询异常'.$exception->getMessage(), 2);
            }
        })->everyMinute();
    }

    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');

        require base_path('routes/console.php');
    }
}
